 package tools.config;

 import java.io.File;
 import java.io.IOException;
 import java.util.Enumeration;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;








 public class ClassUtils
 {
/*  22 */   private static final Logger log = LogManager.getLogger(ClassUtils.class);












   public static boolean isSubclass(Class<?> a, Class<?> b) {
/*  36 */     if (a == b) {
/*  37 */       return true;
     }
/*  39 */     if (a == null || b == null) {
/*  40 */       return false;
     }
/*  42 */     for (Class<?> x = a; x != null; x = x.getSuperclass()) {
/*  43 */       if (x == b) {
/*  44 */         return true;
       }
/*  46 */       if (b.isInterface()) {
/*  47 */         Class<?>[] interfaces = x.getInterfaces();
/*  48 */         for (Class<?> anInterface : interfaces) {
/*  49 */           if (isSubclass(anInterface, b)) {
/*  50 */             return true;
           }
         }
       }
     }
/*  55 */     return false;
   }








   public static boolean isPackageMember(Class<?> clazz, String packageName) {
/*  66 */     return isPackageMember(clazz.getName(), packageName);
   }








   public static boolean isPackageMember(String className, String packageName) {
/*  77 */     if (!className.contains(".")) {
/*  78 */       return (packageName == null || packageName.isEmpty());
     }
/*  80 */     String classPackage = className.substring(0, className.lastIndexOf('.'));
/*  81 */     return packageName.equals(classPackage);
   }










   public static Set<String> getClassNamesFromDirectory(File directory) throws IllegalArgumentException {
/*  94 */     if (!directory.isDirectory() || !directory.exists()) {
/*  95 */       throw new IllegalArgumentException("Directory " + directory + " doesn't exists or is not directory");
     }

/*  98 */     return getClassNamesFromPackage(directory, null, true);
   }









   public static Set<String> getClassNamesFromPackage(File directory, String packageName, boolean recursive) {
/* 110 */     Set<String> classes = new HashSet<>();
/* 111 */     if (!directory.exists()) {
/* 112 */       return classes;
     }
/* 114 */     File[] files = directory.listFiles();
/* 115 */     for (File file : files) {
/* 116 */       if (file.isDirectory()) {

/* 118 */         if (recursive)

         {

/* 122 */           String newPackage = file.getName();
/* 123 */           if (!GenericValidator.isBlankOrNull(packageName)) {
/* 124 */             newPackage = packageName + "." + newPackage;
           }
/* 126 */           classes.addAll(getClassNamesFromPackage(file, newPackage, recursive)); } 
/* 127 */       } else if (file.getName().endsWith(".class")) {
/* 128 */         String className = file.getName().substring(0, file.getName().length() - 6);
/* 129 */         if (!GenericValidator.isBlankOrNull(packageName)) {
/* 130 */           className = packageName + "." + className;
         }
/* 132 */         classes.add(className);
       }
     }
/* 135 */     return classes;
   }











   public static Set<String> getClassNamesFromJarFile(File file) throws IOException {
/* 149 */     if (!file.exists() || file.isDirectory()) {
/* 150 */       throw new IllegalArgumentException("File " + file + " is not valid jar file");
     }

/* 153 */     Set<String> result = new HashSet<>();

/* 155 */     JarFile jarFile = null;
     try {
/* 157 */       jarFile = new JarFile(file);

/* 159 */       Enumeration<JarEntry> entries = jarFile.entries();
/* 160 */       while (entries.hasMoreElements()) {
/* 161 */         JarEntry entry = entries.nextElement();

/* 163 */         String name = entry.getName();
/* 164 */         if (name.endsWith(".class")) {
/* 165 */           name = name.substring(0, name.length() - 6);
/* 166 */           name = name.replace('/', '.');
/* 167 */           result.add(name);
         }
       }
     } finally {
/* 171 */       if (jarFile != null) {
         try {
/* 173 */           jarFile.close();
/* 174 */         } catch (IOException e) {
/* 175 */           log.error("Failed to close jar file " + jarFile.getName(), e);
         }
       }
     }

/* 180 */     return result;
   }
 }


/* Location:              C:\PGX\Server.jar!\tools\config\ClassUtils.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */